﻿#include "combinesplittableview.h"

CombineSplitTableView::CombineSplitTableView(QWidget *parent) : MyTableView(parent)
{
    setEditTriggers(QAbstractItemView::NoEditTriggers);
    setSelectionMode(QAbstractItemView::SingleSelection);
    model = new QStandardItemModel();
    setModel(model);

    header<<"总成商品ID"
        <<"总成商品型号"
       <<"总成商品名称"
      <<"配件商品"
     <<"公式";

    QAction *actionDelete = new QAction("删除", this);

    connect(actionDelete, &QAction::triggered,
            [=]()
    {
        Formula *curFormula = this->currentFormula();
        if(curFormula != nullptr){
            productService.deleteFormula(this, curFormula->getCombineProduct()->productId);
        }
    });

    menu.addAction(actionDelete);

    model->setHorizontalHeaderLabels(header);

    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, &QTableView::customContextMenuRequested, this, &CombineSplitTableView::showMenu);

    connect(&productService, &ProductService::deleteFormulaSuccess,
            [=]()
    {
        this->refreshFormula();
    });
}

void CombineSplitTableView::showMenu(const QPoint point){
    if(model->rowCount() != 0){
        QModelIndex index = indexAt(point);
        if(index.isValid()){
            menu.exec(QCursor::pos());
        }
    }
}

void CombineSplitTableView::insertFormula(Formula &formula){
    QList<QStandardItem*> rowData;

    Product *combineProduct = formula.getCombineProduct();

    if(combineProduct == nullptr){
        return;
    }

    rowData.append(new QStandardItem(combineProduct==nullptr?"":combineProduct->productId));
    rowData.append(new QStandardItem(combineProduct==nullptr?"":combineProduct->type));
    rowData.append(new QStandardItem(combineProduct==nullptr?"":combineProduct->name));

    QString splitNames = "";
    QString formulaDesc = combineProduct==nullptr?"":combineProduct->name+"*"+QString("%1=").arg(combineProduct->count);
    QList<Product*> products = formula.getSplitProducts();
    int i=0;
    for(;i<products.size()-1;i++){
        splitNames.append(products[i]->name+"+");
        formulaDesc.append(products[i]->name+"*"+QString("%1+").arg(products[i]->count));
    }
    splitNames.append(products[i]->name);
    formulaDesc.append(products[i]->name+"*"+QString("%1").arg(products[i]->count));

    rowData.append(new QStandardItem(splitNames));
    rowData.append(new QStandardItem(formulaDesc));

    model->appendRow(rowData);
    formulas.append(&formula);
}

void CombineSplitTableView::insertFormula(QList<Formula*> formulas){
    for(Formula *formula:formulas){
        insertFormula(*formula);
    }
}

void CombineSplitTableView::loadFormulas(){
    DbHelper helper;

    for(FormulaBase *formulabase:helper.getAllFormulas()){
        Formula *formula = new Formula(formulabase);
        insertFormula(*formula);
    }
}

Formula* CombineSplitTableView::currentFormula(){
    int row = currentIndex().row();

    if(row != -1){
        return  formulas[row];
    }else{
        return nullptr;
    }
}

void CombineSplitTableView::clear(){
    formulas.clear();
    model->clear();
    model->setHorizontalHeaderLabels(header);
}
